package uinv.com.uitl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.sf.json.JSONObject;

public class Uitl {

	public String sum(int max, int min) {
		int sum = (int) (min + Math.random() * (max - min + 1));
		String sumstring = Integer.toString(sum);
		return sumstring;
	}
	
	public int sumint(int max, int min) {
		int sum = (int) (min + Math.random() * (max - min + 1));
		return sum;
	}

	public boolean isInTime(String sourceTime, String curTime) {
		if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
			throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
		}
		if (curTime == null || !curTime.contains(":")) {
			throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
		}
		String[] args = sourceTime.split("-");
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		try {
			long now = sdf.parse(curTime).getTime();
			long start = sdf.parse(args[0]).getTime();
			long end = sdf.parse(args[1]).getTime();
			if (args[1].equals("00:00")) {
				args[1] = "24:00";
			}
			if (end < start) {
				if (now >= end && now < start) {
					return false;
				} else {
					return true;
				}
			} else {
				if (now >= start && now < end) {
					return true;
				} else {
					return false;
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
			throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
		}
	}
	public JSONObject readTxtFileIntoStringArrList(String filePath)
	  {
		JSONObject json = null;
	    try
	    {
	      String encoding = "UTF-8";
	      File file = new File(filePath);
	      if ((file.isFile()) && (file.exists()))
	      {
	    	InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
	    	BufferedReader reader=new BufferedReader(read);
	    	StringBuilder sb=new StringBuilder();
	    	String line=null;
	    	while((line=reader.readLine())!=null){
	    	//把换行符去掉
	    	  sb.append(line);
	    	}
	    	reader.close();
	    	//第二步把这个sb里面的真正的json内容拿到也就是从第一个"{"到最后一个"}"包括这两个{};
	    	//这个是我们要的
	    	String jsonString=sb.substring(sb.indexOf("{"),sb.lastIndexOf("}")+1);
	    	//接下来给他转换成json-lib里的JSONObject对象
	    	json=JSONObject.fromObject(jsonString);
	    	System.out.println("读取成功");
	    	System.out.println("-------------------------------------");
	      }
	      else
	      {
	        System.out.println("找不到指定的文件");
	      }
	    }
	    catch (Exception e)
	    {
	      System.out.println("读取文件内容出错");
	      e.printStackTrace();
	    }

	    return json;
	  }
	public boolean writeFile(String data, String filePath)
	  {
	    try
	    {
	      File file = new File(filePath);

	      if (!file.exists()) {
	        file.createNewFile();
	      }

	      FileWriter fileWritter = new FileWriter(file);
	      BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
	      bufferWritter.write(data);
	      bufferWritter.close();
	      return true;
	    }
	    catch (IOException e) {
	      e.printStackTrace();
	    }
		return false;
	  }
}
